# -*- coding: utf-8 -*-
#
#	Copyright (C) 2011 by Igor E. Novikov
#	
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>. 

import sys

from PyQt4 import QtGui, Qt

from uc2.utils import system

from fe import events, app_conf
from fe import config
import mainwindow, iconfactory, view


class Application(QtGui.QApplication):

	"""Provides main Formats Explorer application instance."""

	proxy = None
	config = None
	appdata = None
	mw = None
	generic_icons = {}

	current_doc = None
	docs = []

	def __init__(self, packagedir):
		"""
		Constructs an instance. 
		
		packagedir - Formats Explorer package location. 
		The value is needed for correct resource uploading.
		"""

		QtGui.QApplication.__init__(self, sys.argv)

		self.events = events
		self.appdata = app_conf.AppData()
		self.config = config
		self.config.load(self.appdata.app_config)

		self.generic_icons = iconfactory.get_app_icons(self)

		import actions
		self.actions = actions.create_actions(self)

		self.mw = mainwindow.MainWindow(self)

	def run(self):
		events.emit(events.NO_DOCS)
		msg = 'To start open existing document'
		events.emit(events.APP_STATUS, msg)
		self.mw.show()
		self.exec_()

	def update_config(self):
		if self.mw.windowState() == Qt.Qt.WindowMaximized:
			if self.config.os != system.MACOSX:
				self.config.mw_maximized = 1
		else:
			self.config.mw_maximized = 0

			self.config.mw_width = self.mw.width()
			self.config.mw_height = self.mw.height()

	def app_exit(self):
		self.update_config()
		self.config.save(self.appdata.app_config)
		sys.exit(self.exit())

	def close(self, view):
		pass
		return True

	def new(self):
		doc = view.ModelView(self)
		self.docs.append(doc)
		self.set_current_doc(doc)
#		self.mw.menubar.rebuild_window_menu()
#		self.mw.set_title()
		events.emit(events.APP_STATUS, self.tr('New document created'))

	def open(self):
		pass

	def save_as(self):
		pass

	def set_current_doc(self, doc):
		self.current_doc = doc
		events.emit(events.DOC_CHANGED, doc)


